package com.dc.project.entity.po;

import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import com.own.component.common.base.entity.po.BasePo;
import com.own.constant.ConstantBeanSuffix;
import com.own.util.FileUtil;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.ToString;
import lombok.experimental.Accessors;
import org.apache.commons.lang3.StringUtils;

import java.io.Serializable;

/**
 * TaskFileCommit
 *
 * @author chenxueli
 * @date 2025-03-10 16:37:53
 */
@Schema(title = "任务文件提交" + ConstantBeanSuffix.PO)
@Getter
@Setter
@ToString
@Accessors(chain = true)
@AllArgsConstructor
@NoArgsConstructor
@TableName(value = "t_task_file_commit")
public class TaskFileCommit extends BasePo implements Serializable {

    /**
     * 任务id
     */
    private Long fkTaskId;

    /**
     * 详情id
     */
    private Long fkDetailId;

    /**
     * 项目id
     */
    private Long fkItemId;

    /**
     * 父级提交id
     */
    private Long fkParentCommitId;

    /**
     * 上传的附件id
     */
    private Long fkFileId;

    /**
     * 上传的附件路径
     */
    private String filePath;

    /**
     * 提交类型 1=task 2=detail 3=item
     */
    private Integer type;

    /**
     * 提交的目标id
     */
    private Long fkTargetId;

    /**
     * 解析状态 1=等待执行中 2=解析文本中 3=解析完成，压缩内容中 4=处理完成 5=解析失败 6=压缩内容失败 7=处理失败
     */
    private Integer status;

    /**
     * 解析后的消息内容
     */
    private String parseMessage;

    /**
     * 压缩后的消息内容
     */
    private String compressMessage;

    /**
     * 资料所属年份
     */
    @TableField("`year`")
    private String year;

    /**
     * 资料所属类别
     */
    private String category;

    /**
     * 序号
     */
    private Integer sortOrder;

    public interface Status {
        /**
         * 等待执行中
         */
        int WAITING = 1;
        /**
         * 解析文本中
         */
        int PARSING = 2;
        /**
         * 解析完成，压缩内容中
         */
        int COMPRESSING = 3;
        /**
         * 处理完成
         */
        int PROCESSED = 4;
        /**
         * 解析失败
         */
        int PARSE_FAILED = 5;
        /**
         * 压缩内容失败
         */
        int COMPRESS_FAILED = 6;
        /**
         * 处理失败
         */
        int PROCESS_FAILED = 7;
    }

    public Structure toStructure() {
        return new Structure(this);
    }

    @Getter
    public static class Structure {

        /**
         * commitId
         */
        private final Long id;

        /**
         * 上传的附件路径
         */
        private final String type;

        /**
         * 文本内容
         */
        private final String content;

        public Structure(TaskFileCommit commit) {
            this.id = commit.getId();
            this.type = FileUtil.getSuffix(commit.getFilePath());
            // 优先使用压缩后的文本内容
            this.content = StringUtils.isNotBlank(commit.getCompressMessage())
                    ? commit.getCompressMessage()
                    : commit.getParseMessage();
        }

    }

}
